
#ifndef WRITE_PROCESS_H
#define WRITE_PROCESS_H

#include <string>
#include "datakit.h"
#include <memory>
#include "WriterBase.hpp"

namespace  AMCAX::Exchange 
{
    /*
     * @brief InstanceInfo is used to store the prototype name and transformation matrix of an instance from assembly.
     */
    struct InstanceInfo
    {
        InstanceInfo(Dtk_string name, Dtk_transfo transfo, bool isAssembly, Dtk_ID parentId = 0)
            : PrototypeName(name), Transformation(transfo), IsAssembly(isAssembly), ParentId(parentId) {}
        Dtk_string PrototypeName;
        Dtk_transfo Transformation;
        bool IsAssembly = false;
        Dtk_ID ParentId = 0;
    };


    class WriteProcessBase
    {
    public:
        WriteProcessBase() = default;
        explicit WriteProcessBase(Dtk_string inputFile, std::unique_ptr<WriterBase> writer = nullptr);
        virtual ~WriteProcessBase() = default;

        //write through assemblies
        virtual Dtk_ErrorStatus WriteDocument(Dtk_MainDocPtr inDocument, const std::string& outputDir);
        virtual Dtk_ErrorStatus WriteComponent(Dtk_ComponentPtr inComponent, const Dtk_transfo &inMatrix, bool bCreateFile = true);
        virtual Dtk_ErrorStatus WriteInstance(Dtk_ComponentPtr inComponent);
        virtual Dtk_ErrorStatus WritePrototype(Dtk_ComponentPtr inComponent, const Dtk_transfo &inMatrix, Dtk_ID& outPrototypeId);
        virtual void WriteBody(Dtk_NodePtr inNode, Dtk_tab<Dtk_ID>& outIds);

    public:
        //write node data prototypes
        void WriteMetaData(Dtk_NodePtr inNode);
        void WriteDrawing(Dtk_NodePtr inNode);
        void WriteMesh(Dtk_NodePtr inNode, Dtk_tab<Dtk_ID>& outIds);
        void WriteKinematics(Dtk_NodePtr inNode);
        void WriteFdt(Dtk_NodePtr inNode);
        void WriteFeature(Dtk_NodePtr inNode);
        void WriteAxisSystem(Dtk_NodePtr inNode);
        void WriteLayerFilter(Dtk_NodePtr inNode);
        void WriteCamera(Dtk_NodePtr inNode);
        void WriteModelDisplay(Dtk_NodePtr inNode);
        //void WriteNodeConnector(Dtk_NodePtr inNode);
        void WriteConstraints(Dtk_NodePtr inNode);
        void WriteColorPalette(Dtk_NodePtr inNode);
        Dtk_ErrorStatus WriteNode(Dtk_NodePtr inNode, Dtk_tab<Dtk_ID>& outIds);

        
        DtkErrorStatus InitTesselation();
        void EndTesselation();
        int GetTesselationStatus();

     protected:
        virtual void WriteDtk_Mesh(Dtk_MeshPtr inMeshToWrite, Dtk_tab<Dtk_ID>& outIds);
        Dtk_string m_inputFile;
        std::unique_ptr<WriterBase> m_fileWriter;
        Dtk_transfo m_currentMatrix = Dtk_transfo();
        int m_tessStatus = dtkTessellationNotStarted;        
    };
}

#endif